local STATUE_LIBERTY_INDEX = GameInfo.Buildings['BUILDING_STATUE_LIBERTY'].Index;
local HARBOR_INDEX = GameInfo.Districts['DISTRICT_HARBOR'].Index;
local HARBOR_INDEX_1 = GameInfo.Districts['DISTRICT_ROYAL_NAVY_DOCKYARD'].Index;
function StatueLibertyWonderCompleted(x, y, buildingId, playerId, cityId, percentComplete, unknown)
	if buildingId == STATUE_LIBERTY_INDEX then
		local player = Players[playerId];
        for row in GameInfo.DistrictReplaces() do
            if row.ReplacesDistrictType == 'DISTRICT_HARBOR' then
                HARBOR_INDEX_2 = GameInfo.Districts[row.CivUniqueDistrictType].Index;
                -- print(HARBOR_INDEX_1,row.CivUniqueDistrictType);
            end
        end
		for _, city in player:GetCities():Members() do
			if city:GetDistricts():HasDistrict(HARBOR_INDEX) or city:GetDistricts():HasDistrict(HARBOR_INDEX_1) or city:GetDistricts():HasDistrict(HARBOR_INDEX_2)then
				local canGrant = {};
				for row in GameInfo.Buildings() do
					if (row.PrereqDistrict == 'DISTRICT_HARBOR') and (not row.IsWonder) and (not row.MustPurchase) then
						print(row.BuildingType);
						local unlocked = true;
						local hasPrereq = false;
						local prereqMet = false;
						if row.PrereqTech ~= nil then
							local techInfo = GameInfo.Technologies[row.PrereqTech];
							unlocked = unlocked and player:GetTechs():HasTech(techInfo.Index);
						end
						if row.PrereqCivic ~= nil then
							local civicInfo = GameInfo.Civics[row.PrereqCivic];
							unlocked = unlocked and player:GetCulture():HasCivic(civicInfo.Index);
						end
						for row2 in GameInfo.BuildingPrereqs() do
							if row2.Building == row.BuildingType then
								hasPrereq = true;
								local buildingInfo = GameInfo.Buildings[row2.PrereqBuilding];
								prereqMet = prereqMet or city:GetBuildings():HasBuilding(buildingInfo.Index);
							end
						end
						if unlocked and ((not hasPrereq) or prereqMet) then
							table.insert(canGrant, {
								buildingId = row.Index,
								cost = row.Cost
							});
						end
					end
				end
				table.sort(canGrant, function (b1, b2) return b1.cost > b2.cost; end);
				if #canGrant > 0 then
					local buildingId = canGrant[1].buildingId;
					city:GetBuildQueue():CreateBuilding(buildingId);
				end
			end
		end
	end
end

Events.WonderCompleted.Add(StatueLibertyWonderCompleted);